package com.chuangshi.core.vote.member.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 会员消息队列监听
 *
 * @author marcus
 * @date 2018-11-20
 */
@Component
public class MemberRabbitListener {
    @Bean
    public Queue v1MemberSaveQueue() {
        return QueueBuilder.durable("ec.member.v1.save").build();
    }

    @Bean
    public Binding v1MemberSaveBinding(Exchange exchange) {
        return BindingBuilder.bind(v1MemberSaveQueue()).to(exchange).with("ec.member.v1.save").noargs();
    }

    @RabbitListener(queues = {"ec.member.v1.save"})
    public void v1MemberSave(Message message, Channel channel) throws Exception {
        System.out.println("save");
        System.out.println(message);

        Long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
    }

    @Bean
    public Queue v1MemberUpdateQueue() {
        return QueueBuilder.durable("ec.member.v1.update").build();
    }

    @Bean
    public Binding v1MemberUpdateBinding(Exchange exchange) {
        return BindingBuilder.bind(v1MemberUpdateQueue()).to(exchange).with("ec.member.v1.update").noargs();
    }

    @RabbitListener(queues = {"ec.member.v1.update"})
    public void v1MemberUpdate(Message message, Channel channel) throws Exception {
        System.out.println("update");
        System.out.println(message);

        Long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
    }

    @Bean
    public Queue v1MemberDeleteQueue() {
        return QueueBuilder.durable("ec.member.v1.delete").build();
    }

    @Bean
    public Binding v1MemberDeleteBinding(Exchange exchange) {
        return BindingBuilder.bind(v1MemberDeleteQueue()).to(exchange).with("ec.member.v1.delete").noargs();
    }

    @RabbitListener(queues = {"ec.member.v1.delete"})
    public void v1MemberDelete(Message message, Channel channel) throws Exception {
        System.out.println("delete");
        System.out.println(message);

        Long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
    }
}